import router, { asyncRoutes } from '@/router'
import store from '@/store'
// 导入进度条相关插件样式
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
router.beforeEach(async(to, from, next) => {
  // 设置标题
  document.title = getPageTitle(to.meta.title)
  // 一转跳就要加载进度条
  NProgress.start()
  const token = store.state.user.token
  // 定义白名单
  const whiteList = ['/login', '/404']
  if (token) {
  // 有token登陆成功
    if (to.path === '/login') {
      // 有token要去登录页
      next('/')
      // 如果next指定了地址, 则不会触发NProgress.done() 所以需要手动添加防止样式加载无法关闭
      NProgress.done()
      console.log('有token还要去登录页,直接去主页')
    } else {
      console.log('有token 转跳的不是登录页 放行')
      // 在此处调用 actions内的函数发送请求 在进入页面前就获取导数据
      // 转跳之前判断.是否有userID
      if (!store.getters.userId) {
        // store.dispatch('user/actions里的函数')
        const menuList = await store.dispatch('user/getUserInfo')
        console.log('menuListkkkkkkkkkk', menuList)
        console.log('asyncRoutesasyncRoutes', asyncRoutes)
        const filterRoutes = asyncRoutes.filter(routes => {
          // 筛选出 所以的路由规则的名字
          const routesName = routes.children[0].name
          // 返回此用户所拥有的路由规则筛选
          return menuList.includes(routesName)
        })
        filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
        // 在此处动态添加路由
        router.addRoutes(filterRoutes)
        // 向Vux内 存入 动态路由的数组
        store.commit('menu/setMenuList', filterRoutes)
        next({
          // 保证所有的动态路由添加完成后 再跳转页面
          ...to,
          replace: true
        })
      } else {
        next()
      }
    }
  } else {
  // 没有token
    if (whiteList.includes(to.path)) {
      next()
      console.log('有白名单,放行')
    } else {
      next('/login')
      NProgress.done()
      console.log('没有白名单,转跳登录页')
    }
  }
})
// 关闭进度条
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
